﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XMM.Dialoges.Library
{
    /// <summary>
    /// Interaktionslogik für CoverArtEditor.xaml
    /// </summary>
    public partial class CoverArtEditor : Window
    {
        /// <summary>
        /// Path to current cover-art-image.
        /// </summary>
        string CurrentImagePath { get; set; }

        /// <summary>
        /// Path to selected cover-art-image.
        /// </summary>
        string NewImagePath { get; set; }

        /// <summary>
        /// Image-Control of the selected cover-art-image.
        /// </summary>
        Image NewImage { get; set; }

        /// <summary>
        /// Reference to parent Holster_CoverArt()-Control.
        /// </summary>
        Controls.Library.Holster_CoverArt ParentCoverArtHolster { get; set; }

        /// <summary>
        /// Initialize a new CoverArtEditor()-Dialoge.
        /// </summary>
        /// <param name="ctlParentCoverArtHolster">(Controls.Library.Holster_CoverArt) parent CoverArt-Holster-Control.</param>
        /// <param name="sCurrentImagePath">(string) Path to current Image-File (If any, otherwise use String.Empty).</param>
        public CoverArtEditor(Controls.Library.Holster_CoverArt ctlParentCoverArtHolster, string sCurrentImagePath)
        {
            InitializeComponent();

            CurrentImagePath = sCurrentImagePath;
            ParentCoverArtHolster = ctlParentCoverArtHolster;
        }

        /// <summary>
        /// Close this CoverArtEditor()-Dialog.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Load existing Cover-Art-Image into preview.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.IO.File.Exists(CurrentImagePath))
            {
                try
                {
                    imgCurrentCover.Source = new BitmapImage(new Uri(CurrentImagePath, UriKind.Absolute));
                    if (CurrentImagePath.Length > 50)
                        lblCurrentPath.Content = CurrentImagePath.Substring(CurrentImagePath.Length - 50, 50);
                    else
                        lblCurrentPath.Content = CurrentImagePath;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Beim Laden des temp. Covers/Posters ist ein Fehler aufgetreten: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                lblCurrentPath.Content = String.Empty;
            }
        }

        /// <summary>
        /// Browse for local stored image-file to load as new CoverArt-Image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchLocalImage_Click(object sender, RoutedEventArgs e)
        {
            // Setup File-Open-Dialoge.
            System.Windows.Forms.OpenFileDialog dlgOpenCoverImage = new System.Windows.Forms.OpenFileDialog();
            dlgOpenCoverImage.Filter = "JPEG-Dateien|*.jpg|PNG-Bilder|*.png|Bitmap-Bilder|*.bmp|GIF-Bilder|*.gif|Alle Unterstützten|*.jpg,*.png,*.bmp,*.gif";
            if (dlgOpenCoverImage.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Reset old values of NewImage and imgNewCover.Source.
                NewImage = null;
                imgNewCover.Source = null;

                // Check if new file exists.
                if (System.IO.File.Exists(dlgOpenCoverImage.FileName))
                {
                    try
                    {
                        // if new image-file exists, try to load it into preview.
                        NewImage = new Image();
                        NewImage.Source = new BitmapImage(new Uri(dlgOpenCoverImage.FileName, UriKind.Absolute));
                        if (NewImage.Source != null)
                        {
                            // If image is valid, update Preview and NewImagePath.
                            NewImagePath = dlgOpenCoverImage.FileName;
                            imgNewCover.Source = NewImage.Source;
                        }
                    }
                    catch (Exception ex)
                    {
                        NewImage = null;
                        NewImagePath = String.Empty;
                        imgNewCover.Source = null;
                    }
                }
            }
        }

        /// <summary>
        /// Save new CoverArt-Image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            // Check if any image was selected.
            if (NewImage != null)
            {
                try
                {
                    // If any image was selected, set it as new CoverArt-Image.
                    BitmapImage bmpNewCover = (BitmapImage)NewImage.Source;
                    if (bmpNewCover != null)
                    {
                        ParentCoverArtHolster.setCoverImage(NewImagePath);
                        ParentCoverArtHolster.setStatusImage(true);
                        Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Das Bild kann nicht gespeichert werden: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                if (MessageBox.Show("Es wird kein Bild für diesen Film gespeichert! Sind Sie sicher?", "Kein Bild verwenden?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    ParentCoverArtHolster.setCoverImage(String.Empty);
                    ParentCoverArtHolster.setStatusImage(false);

                    Close();
                }
            }
        }

        private void btnRotateCounterClockwise_Click(object sender, RoutedEventArgs e)
        {
            if (imgNewCover.Source != null)
            {
                BitmapImage bmpNewCover = (BitmapImage)imgNewCover.Source;

                bmpNewCover.Rotation = Tools.getRotationNewMode(bmpNewCover.Rotation, false);
                imgNewCover.Source = bmpNewCover;
            }
        }

        private void btnRotateClockwise_Click(object sender, RoutedEventArgs e)
        {
            if (imgNewCover.Source != null)
            {
                BitmapImage bmpNewCover = (BitmapImage)imgNewCover.Source;

                Rotation oRotation = Tools.getRotationNewMode(bmpNewCover.Rotation, true);
                bmpNewCover.Rotation = oRotation;
                imgNewCover.Source = bmpNewCover;
            }
        }
    }
}
